package service.impl;

import java.util.List;

import security.MySecurityContextHandle;
import service.interf.IAgentBaseDef;

import common.impl.exception.ServiceException;

import dao.interf.IAgentBaseDefDAO;
import dao.pojo.AgentBaseDef;

public class AgentBaseDefImpl extends BaseDataServiceImpl implements
		IAgentBaseDef {

	private IAgentBaseDefDAO agentBaseDefDao;

	public void setAgentBaseDefDao(IAgentBaseDefDAO agentBaseDefDao) {
		this.agentBaseDefDao = agentBaseDefDao;
	}

	public String get() throws Exception {
		List<AgentBaseDef> agentBaseDefL = agentBaseDefDao.findAll(null);
		AgentBaseDef agentBaseDef;
		Integer agentBase = 600;
		if (null == agentBaseDefL || agentBaseDefL.isEmpty()) {
			agentBaseDef = new AgentBaseDef();
			agentBaseDef.setAgentBase(600);
			agentBaseDefDao.save(agentBaseDef);
		} else {
			agentBase = agentBaseDefL.get(0).getAgentBase();
		}
		if (null == agentBase)
			agentBase = 600;
		return agentBase.toString();
	}

	public String edit(Integer agentBase) throws Exception {
		// 验证
		String userName = MySecurityContextHandle.getUserName();
		if(!"admin".equals(userName))
			throw new ServiceException("请先登录");
		if (null == agentBase)
			agentBase = 0;
		if (agentBase < 1)
			throw new ServiceException("基数必须大于0");
		agentBaseDefDao.deleteAll();
		AgentBaseDef agentBaseDef = new AgentBaseDef();
		agentBaseDef.setAgentBase(agentBase);
		agentBaseDefDao.save(agentBaseDef);
		return "1";
	}

}
